//
//  QuestionFeedBackVC.m
//  Learn
//
//  Created by zyl_macbookpro on 2024/4/1.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "QuestionFeedBackVC.h"
#import "UpdateAppAlertView.h"
#import "MineAPIManager.h"
@interface QuestionFeedBackVC ()<UITableViewDelegate,UITableViewDataSource,YYTextViewDelegate>

@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic ,strong)UIView *tableHeaderView;

@property(nonatomic ,strong)UIView *tableFootorView;

@property(nonatomic ,strong)NSArray *titleArray;

/* 内容 **/
@property (nonatomic ,strong)YYTextView *contentTextView;

@end

@implementation QuestionFeedBackVC
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self renderNavBarWithBackgroundImage:[UIImage imageWithColor:lClearColor] backgroundColor:lClearColor shadowColor:nil textAttributeDic:@{NSForegroundColorAttributeName:rgba(51, 51, 51, 1),NSFontAttributeName:MFontWithSize(18)} enableScrollEdgeAppearance:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"意见反馈";
    // Do any additional setup after loading the view.
    [self setBackButtonIsHidden:NO];
    [self.view addSubview:self.tableView];
    self.view.backgroundColor = rgba(245, 245, 245, 1);

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self tapClick];
}
- (void)tapClick
{
    [self.view endEditing:YES];
}
- (void)submitBtnAction
{
    [self tapClick];
    if (self.contentTextView.text.length == 0) {
        return;
    }
  
    [MineAPIManager AddRequestFeedbackWithParam:@{@"content":self.contentTextView.text} result:^(NSDictionary * _Nonnull dic) {
        if ([dic[@"code"] intValue] == 1) {
            [SVProgressHUD showSuccessWithStatus:@"反馈提交成功！"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}
- (void)textViewDidChange:(YYTextView *)textView
{
    if (textView.text.length > 500) {
        textView.text = [textView.text substringToIndex:498];
    }
}
#pragma mark -UITableViewDelegate,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 0;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 62;
    
}


#pragma mark -getters and setters
-(UITableView *)tableView{
    if (!_tableView){
        _tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, lTopBarHeight, lScreenWidth, lScreenHeight - lTopBarHeight) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        _tableView.bounces = NO;
        [_tableView setBackgroundColor:lClearColor];
        [_tableView setSeparatorColor:rgba(229, 229, 229, 1)];
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 10, 0, 0)];
        [_tableView setTableFooterView:self.tableFootorView];
        [_tableView setTableHeaderView:self.tableHeaderView];
    }
    
    return _tableView;
}
-(UIView *)tableFootorView
{
    if (!_tableFootorView)
    {
        _tableFootorView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth, 146)];
        _tableFootorView.backgroundColor = lClearColor;
        UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        submitBtn.frame =CGRectMake(0, 0, lScreenWidth - 20, 45);
       
        submitBtn.selected =NO;
        [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
        [submitBtn setTitleColor:lWhiteColor forState:UIControlStateNormal];
        [submitBtn.titleLabel setFont:MFontWithSize(18)];
        [submitBtn addTarget:self action:@selector(submitBtnAction) forControlEvents:UIControlEventTouchUpInside];
        submitBtn.backgroundColor = lClearColor;
        UIView *btnView =[[UIView alloc] initWithFrame:CGRectMake((lScreenWidth - submitBtn.width)/2.0, 60, submitBtn.width, submitBtn.height)];
        [btnView setAllCornersWithCornerRadius:submitBtn.height/2.0];
        // gradient
        CAGradientLayer *gl = [CAGradientLayer layer];
        gl.frame = CGRectMake(0,0,btnView.width,btnView.height);
        gl.startPoint = CGPointMake(1, 0.5);
        gl.endPoint = CGPointMake(0, 0.5);
        gl.colors = @[(__bridge id)[UIColor colorWithRed:41/255.0 green:241/255.0 blue:156/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:2/255.0 green:161/255.0 blue:249/255.0 alpha:1.0].CGColor];
        gl.locations = @[@(0), @(1.0f)];
        [btnView.layer addSublayer:gl];
        [btnView addSubview:submitBtn];
        [_tableFootorView addSubview:btnView];
        [_tableFootorView setHeight:btnView.bottom];
       
    }
    
    return _tableFootorView;
    
}

-(UIView *)tableHeaderView
{
    if (!_tableHeaderView)
    {
        _tableHeaderView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth, 146)];
        
        _tableHeaderView.backgroundColor = lClearColor;
        UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(10, 10, lScreenWidth - 20, 300)];
        [contentView setAllCornersWithCornerRadius:15];
        contentView.layer.borderWidth = 1;
        contentView.layer.borderColor = rgba(229, 229, 229, 1).CGColor;
        
        UILabel *label = [[UILabel alloc] init];
        label.frame = CGRectMake(5,self.contentTextView.bottom + 8,lScreenWidth - 20 - 10,22);
        label.numberOfLines = 0;
        [self.view addSubview:label];
        label.textColor = rgba(181, 181, 181, 1);
        label.font = MFontWithSize(16);
        label.text = @"（500字以内）";
        label.textAlignment = NSTextAlignmentRight;
        [contentView addSubview:self.contentTextView];
        [contentView addSubview:label];
        [_tableHeaderView addSubview:contentView];
        [_tableHeaderView setHeight:label.bottom + 10];
        
    }
    
    return _tableHeaderView;
    
}
-(YYTextView *)contentTextView
{
    if (!_contentTextView) {
        _contentTextView = [[YYTextView alloc] initWithFrame:CGRectMake(15, 5, lScreenWidth - 20 - 30, 255)];
        _contentTextView.font = MFontWithSize(18);
        _contentTextView.textAlignment = NSTextAlignmentJustified;
        _contentTextView.placeholderText = @"请描述你的问题";
        _contentTextView.placeholderTextColor = rgba(181, 181, 181, 1);
        _contentTextView.delegate = self;
        _contentTextView.textColor = rgba(51, 51, 51, 1);
//        _contentTextView.backgroundColor = KyellowColor;
    }
    return _contentTextView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
